import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { usePermissStore } from "../store/permiss";
import Home from "../views/home.vue";
import menu from "../router/menu";
import account from "../router/account";
const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/accountlist",
  },
  {
    path: "/",
    name: "Home",
    component: Home,
    children: [...menu, ...account],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: "登录",
    },
    component: () =>
      import(/* webpackChunkName: "login" */ "../views/login.vue"),
  },
  {
    path: "/403",
    name: "403",
    meta: {
      title: "没有权限",
    },
    component: () => import(/* webpackChunkName: "403" */ "../views/403.vue"),
  },
  {
    path: "/:catchAll(.*)",
    name: "NotFound",
    meta: {
      title: "找不到页面",
    },
    component: () => import(/* webpackChunkName: "403" */ "../views/404.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  document.title = `${to.meta.title}`;
  const token = localStorage.getItem("token");
  if (to.path === "/login") {
    next();
  } else if (token) {
    next();
  } else {
    next("/login");
  }
});

export default router;
